//
//  MJHomeTableViewController.swift
//  新浪微博
//
//  Created by MaJi on 16/4/6.
//  Copyright © 2016年 MaJi. All rights reserved.
//
import SVProgressHUD
import UIKit
let cellID = "cellID"
class MJHomeTableViewController: MJbaseTableViewController {

    private lazy var statuelistViewModel:MJStatuelistViewModel = MJStatuelistViewModel()
    //        自定义顶部刷新control
    let myRefresh:MJRefreshController = MJRefreshController()
    override func viewDidLoad() {
        super.viewDidLoad()

        

        setupUI()
        //        加载数据
        lodaData()
    }

    
    private func setupUI(){
//        进行判断是否为第一次登录,是否设置访客视图
        if !MJUserAccountViewModel.sharedAccount.userLogin {
            // 设置访客视图信息 如果不加判断且return ,tableview是nil,但会执行.导致致命错误,强制解包有空值
            visitorView?.setVisitorInfo("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)
          return
        }
//        加载tableView视图
//        设置导航条上的自定义按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName:"navigationbar_pop", target: self, action: "pop")
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: nil, imageName: "navigationbar_friendsearch", target: self, action: "push")
        prepareForTableView()
//        添加底部菊花转view
        tableView.tableFooterView = indicatorView
////        添加顶部菊花,系统的方法 一般在工作中,需要自定义才能满足需求
//        refreshControl = UIRefreshControl()
//        refreshControl?.addTarget(self, action: "lodaData", forControlEvents: .ValueChanged)
        tableView.addSubview(myRefresh)
        myRefresh.addTarget(self, action: "lodaData", forControlEvents: .ValueChanged)
        
    }
//    添加底部的菊花转view
    lazy var indicatorView:UIActivityIndicatorView = {
       let v = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        
        return v
    }()
    @objc private func pop(){
        let temp = MJTempViewController()
        navigationController?.pushViewController(temp, animated: true)
    }
    
    @objc private func push(){
        let temp = MJTempViewController()
        navigationController?.pushViewController(temp, animated: true)
    }

    private func prepareForTableView(){
//    注册cell
        tableView.registerClass(MJStatusCell.self, forCellReuseIdentifier: cellID)
//        设置cell的行高为自动计算
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300
//        tableView.rowHeight = 250
        tableView.separatorStyle = .None
        
    }
    
   @objc private func lodaData(){
//        向viewModel请求数据

        statuelistViewModel.lodaHomeData(indicatorView.isAnimating()) { (isSuccess) -> () in
            if !isSuccess {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
            }
            //不管成功还是失败都是应该停止refreshControl的转动 并且回到最顶部
//            self.refreshControl?.endRefreshing()//系统的
            self.myRefresh.endRefreshing()
//            停止菊花转动
            self.indicatorView.stopAnimating()
            
            //            刷新数据
            self.tableView.reloadData()
        }

    }
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuelistViewModel.statuses.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! MJStatusCell
//        cell.textLabel?.text = statueViewModel.statuses[indexPath.row].user?.name
        cell.statusViewModel = statuelistViewModel.statuses[indexPath.row]
//        当是最后一个cell时下来加载更多数据 隐式加载更多
        if indexPath.row == self.statuelistViewModel.statuses.count-1 && !indicatorView.isAnimating(){
//            加载数据
            indicatorView.startAnimating()
            self.lodaData()
        }
        return cell
    }
    

}
